package com.atguigu.admin.controller;

import com.atguigu.admin.bean.User;
import com.atguigu.admin.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Arrays;
import java.util.List;
@Slf4j
@Controller
public class TableController {
    @Autowired
    UserService userService;
    @GetMapping("/basic_table")
    public String basic_table(){
        return "table/basic_table";
    }

    @GetMapping("/user/delete/{id}")
    public String delete(@PathVariable("id") Long id,
                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                         RedirectAttributes ra){
        log.info("删除用户{}", id);
        userService.removeById(id);
        ra.addAttribute("pageNum", pageNum);
        return "redirect:/dynamic_table";
    }
    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pageNum", defaultValue = "1")  Integer pageNum, Model model){

//        List<User> userList = Arrays.asList(
//                new User("zhangsa", "123455"),
//                new User("lisi", "123455"),
//                new User("wangwu", "123455"),
//                new User("zhaoliu", "123455"),
//                new User("tianqi", "123455"));
//        model.addAttribute("users", userList);
        //从数据库中查询user表中的数据
//        userService.list().forEach(user -> {
//           log.info("user:{}", user.getName());
//        });

        List<User> list = userService.list();
        //model.addAttribute("users", list);
        //分页查询对象
        Page<User> userPage = new Page<>(pageNum,2);
        Page<User> page = userService.page(userPage, null);
        long current = page.getCurrent();
        long pages = page.getPages();
        List<User> records = page.getRecords();

        model.addAttribute("page", page);

        return "table/dynamic_table";
    }
    @GetMapping("/responsive_table")
    public String responsive_table(){
        return "table/responsive_table";
    }
    @GetMapping("/editable_table")
    public String editable_table(){
        return "table/editable_table";
    }

}
